package su.Ner;

import java.util.Vector;

import iitb.CRF.DataSequence;

public class NerDataSequence implements DataSequence {

	String[] x;
	/**
	 * the label value:
	 * 0 other word
	 * 1 person first
	 * 2 person middle
	 * 3 person end
	 * 4 location first
	 * 5 location middle
	 * 6 location end
	 * 7 organization first
	 * 8 organization middle
	 * 9 organization end
	 * 10 title first
	 * 11 title middle
	 * 12 title end
	 */
	int[] y;
	public static int labelNum=13;
	public boolean good =true;
	
	public NerDataSequence(){
	}
	
	public NerDataSequence(String sentence){
		String[] sa=sentence.split(" ");
//		if(sa[0].equals("Speaking"))
//			System.out.println();
		Vector<String> v=new Vector<String>();
		for(String s:sa){
			if(null!=s && !s.equals(""))
				v.add(s);
		}
		x=new String[v.size()];
		for(int i=0;i<v.size();i++)
			x[i]=v.get(i);
		y=new int[x.length];
		for(int k=0;k<x.length;k++){
			y[k]=-1;
			
			//normalize digit
			if(isDigit(x[k]))
				x[k]="_DIGIT";
		}
		int i=0;
		while(i<x.length){	
			if(x[i].length()<4){
				y[i]=0;
				i++;
				continue;
			}
				
			if(!( x[i].charAt(0)=='['&& x[i].charAt(2)==']' )){
				y[i]=0;
				i++;
				continue;
			}
			
			
			//is token begin, find token end
			int end=i;
			for(;end<x.length;end++){
				if(x[end].length()<4)
					continue;
				
				int length=x[end].length();
				if( x[end].charAt(length-4)=='['&& x[end].charAt(length-1)==']' )
					break;
			}
			
			//label  i .. end;
			int base=-1;
			if(x[i].charAt(1)=='P')
				base=1;
			else if(x[i].charAt(1)=='L')
				base=4;
			else if(x[i].charAt(1)=='O')
				base=7;
			else if(x[i].charAt(1)=='T')
				base=10;
			else System.exit(-1);
			y[end]=base+2;
			y[i]=base;
			for(int k=i+1;k<end;k++)
				y[k]=base+1;
			
			//delete token
			x[i]=x[i].substring(3,x[i].length());
			x[end]=x[end].substring(0,x[end].length()-4);
			
			i=end+1;
		}
	}
	
	public int length() {
		return x.length;
	}

	public void set_y(int i, int label) {
		y[i]=label;
		
	}

	public Object x(int i) {
		return x[i];
	}

	public int y(int i) {
		return y[i];
	}

	boolean isDigit(String value) {
		  try {
		   Double.parseDouble(value);
		   return true;
		  } catch (NumberFormatException e) {
		   return false;
		  }
		 }

	public NerDataSequence clone(){

		NerDataSequence newSequence =new NerDataSequence();
		newSequence.x=this.x.clone();
		newSequence.y=this.y.clone();
		return newSequence;
	}
	public String toString(){
		NerDataSequence ds =this.clone();
		ds.addTag('P');
		ds.addTag('L');
		ds.addTag('O');
		ds.addTag('T');
		String res="";
		for(String s :ds.x)
			res+=s+" ";
		return res;
	}
	
	public boolean addTag(char t){
		boolean res=true;
		
		int label=-1;
		if(t=='P')
			label=1;
		else if(t=='L')
			label=4;
		else if(t=='O')
			label=7;
		else if(t=='T')
			label=10;
		else
			U.error("not PLOT");
		
		for(int i=0;i<length();i++){
			if(y(i)!=label) continue;
			
			x[i]="["+t+"]"+x(i);
			
			if(i==length()-1 || (y(i+1)!=(label+1)&&y(i+1)!=(label+2))){	//single name
				x[i]=x(i)+"[/"+t+"]";
				continue;
			}
			
			i++;
			for(;i<y.length && y(i)==label+1;i++);
			if(i==y.length){
				i=i-1;
			}
			if(y(i)!=label+2){
				x[i]=y(i)+"!!!"+x[i];
				for(int j=0;j<length();j++ ){
					System.err.print((String)x(j)+'|'+y(j)+' ');
				}
				System.err.println();
				good=false;
				res=false;
			}
			x[i]=x(i)+"[/"+t+"]";
		}
		return res;
	}
	
//	public static void main(String[] args) {
//		NerDataSequence nds=new NerDataSequence("“ You need to start with something , and having this information allows you to try to attempt to look at prevention , ” said [T]Dr.[/T] [P]Keith Siller[/P] , medical [T]director[/T] of the [O]Comprehensive Stroke Care Center[/O] at [O]New York University Langone Medical Center[/O] . “ It may not mean anything now , but how can you even begin to tackle prevention of this disease if you don ' t know what causes it . This is a very important initial step . ”"); 
//		System.out.println(nds.toString());
//	}

	public boolean hasSameLabels(NerDataSequence ds){
		for(int i=0;i<length();i++)
			if (ds.y(i)!=y(i)){
				x[i]=x[i]+"!!!!";
				ds.x[i]=ds.x[i]+"!!!!";
				return false;
			}
		return true;
	}
}
